package com.epmis.epmis.utils;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.util.Base64;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ImageUitl {
    /**
     * 本地图片转换成base64字符串
     * @param imageName	图片名称
     * @reture 图片Base64
     */
    public static String ImageToBase64ByLocal(String imageName) throws IOException {
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            //获取图片路径
            Resource resource = new ClassPathResource("/static/img/"+imageName);
            File file = resource.getFile();
            in = new FileInputStream(file.getPath());

            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        Base64.Encoder encoder = Base64.getEncoder();
        // 返回Base64编码过的字节数组字符串
        return encoder.encodeToString(data);
    }
}
